package com.study.handler;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.study.domain.EmqxClientEvents;
import com.study.event.MqttMsgEvent;
import com.study.service.EmqxClientEventsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;


/**
 * @Description: TODO
 * @Author: csc
 * @Date: 2024/11/30 15:28
 * @Version: 1.0
 **/
@Component
public class MqttMessageHandler {

    @Autowired
    private EmqxClientEventsService emqxClientEventsService;
    /**
     {mqtt_receivedRetained=false, mqtt_id=2, mqtt_duplicate=false, id=9113994f-6ed0-1669-51b2-50f860ad1e5a, mqtt_receivedTopic=/iot/21090509005/realtimeData, mqtt_receivedQos=1, timestamp=1733122292308}
     * @param message
     */
    @ServiceActivator(inputChannel = "mqttInputChannel")
    public void handleMessage(Message<String> message) {

        MessageHeaders headers = message.getHeaders();
        //格式化消息头
        JSONObject hearder = JSON.parseObject(JSON.toJSONString(headers));

        //获取发送的消息
        String payload = message.getPayload();


        MqttMsgEvent mqttMsgEvent = new MqttMsgEvent(new Object(),message);
        //发布事件
        SpringUtil.getApplicationContext().publishEvent(mqttMsgEvent);







    }



}